package com.m3839.sdk.ym.core

import android.content.Context

/**
 * Description:YMStorage
 *
 * @author 陈宝阳
 * @create 2020/4/13 14: 15
 */
class YMStorage private constructor() {

    private object SingletonHolder {
        val INSTANCE = YMStorage()
    }

    /**
     * 存入键值对
     * @param context
     * @param key
     * @param value
     */
    private fun put(
        context: Context,
        key: String,
        value: Any
    ) {
        //判断类型
        val type = value.javaClass.simpleName
        val sharedPreferences = context.getSharedPreferences(
            FILE_NAME,
            Context.MODE_PRIVATE
        )
        val editor = sharedPreferences.edit()
        when (type) {
            "Integer" -> {
                editor.putInt(key, (value as Int))
            }
            "Boolean" -> {
                editor.putBoolean(key, (value as Boolean))
            }
            "Float" -> {
                editor.putFloat(key, (value as Float))
            }
            "Long" -> {
                editor.putLong(key, (value as Long))
            }
            "String" -> {
                editor.putString(key, value as String)
            }
        }
        editor.apply()
    }

    /**
     * 读取键的值，若无则返回默认值
     * @param context
     * @param key
     * @param defValue 默认值
     * @return
     */
    private operator fun get(
        context: Context,
        key: String,
        defValue: Any
    ): Any? {
        val sharedPreferences = context.getSharedPreferences(
            FILE_NAME,
            Context.MODE_PRIVATE
        )
        when (defValue.javaClass.simpleName) {
            "Integer" -> {
                return sharedPreferences.getInt(key, (defValue as Int))
            }
            "Boolean" -> {
                return sharedPreferences.getBoolean(key, (defValue as Boolean))
            }
            "Float" -> {
                return sharedPreferences.getFloat(key, (defValue as Float))
            }
            "Long" -> {
                return sharedPreferences.getLong(key, (defValue as Long))
            }
            "String" -> {
                return sharedPreferences.getString(key, defValue as String)
            }
            else -> return null
        }
    }

    /**
     * 保存防沉迷的状态(防沉迷状态是否已经开启)
     *
     * @param context
     * @param value
     */
    fun saveFcmStatus(context: Context, value: Boolean) {
        put(context, KEY_FCM_OPEN, value)
    }

    /**
     * 获取防沉迷的状态(防沉迷状态是否已经开启)
     *
     * @param context
     * @param defaultValue
     * @return
     */
    fun getFcmStatus(
        context: Context,
        defaultValue: Boolean
    ): Boolean {
        return get(context, KEY_FCM_OPEN, defaultValue) as Boolean
    }

    /**
     * 保存防沉迷的剩余时间
     *
     * @param context
     */
    fun saveFcmRemainTime(
        context: Context,
        key: String,
        value: Long
    ) {
        put(context, key, value)
    }

    /**
     * 获取防沉迷的剩余时间
     *
     * @param context
     * @return
     */
    fun getFcmRemainTime(context: Context, key: String): Long {
        return get(context, key, 0L) as Long
    }

    /**
     * 保存防沉迷的当前无累计时长退出的状态
     *
     * @param context
     */
    fun saveFcmNotTime(
        context: Context,
        key: String,
        value: Boolean
    ) {
        put(context, key + KEY_FCM_NOT_TIME, value)
    }

    /**
     * 获取防沉迷的当前无累计时长退出的状态
     *
     * @param context
     * @return
     */
    fun getFcmNotTime(context: Context, key: String): Boolean {
        return get(context, key + KEY_FCM_NOT_TIME, false) as Boolean
    }

    /**
     * 保存密码
     *
     * @param context
     * @param value
     */
    fun savePassword(context: Context, value: String) {
        put(context, KEY_PASSWORD, value)
    }

    /**
     * 获取密码
     *
     * @param context
     * @param defaultValue
     * @return
     */
    fun getPassword(context: Context, defaultValue: String): String? {
        return get(context, KEY_PASSWORD, defaultValue) as String?
    }

    /**
     * 保存初始密码
     *
     * @param context
     * @param value
     */
    fun saveDefaultPassword(context: Context, value: String) {
        put(context, KEY_DEFAULT_PASSWORD, value)
    }

    /**
     * 获取初始密码
     *
     * @param context
     * @param defaultValue
     * @return
     */
    fun getDefaultPassword(
        context: Context,
        defaultValue: String
    ): String {
        return get(
            context,
            KEY_DEFAULT_PASSWORD,
            defaultValue
        ) as String
    }

    /**
     * 保存初始方向
     *
     * @param context
     * @param value
     */
    fun saveDefaultOrientation(context: Context, value: Int) {
        put(context, KEY_FCM_ORIENTATION, value)
    }

    /**
     * 获取初始方向
     *
     * @param context
     * @param defaultValue
     * @return
     */
    fun getDefaultOrientation(context: Context, defaultValue: Int): Int {
        return get(context, KEY_FCM_ORIENTATION, defaultValue) as Int
    }


    companion object {
        /**
         * 存储文件的文件名
         */
        private const val FILE_NAME = "hykb_single_fcm"

        /**
         * 存储密码的键值key
         */
        private const val KEY_PASSWORD = "hykb_password"

        /**
         * 存储初始密码的键值key
         */
        private const val KEY_DEFAULT_PASSWORD = "hykb_default_password"

        /**
         * 存储是否开启防沉迷的键值key
         */
        private const val KEY_FCM_OPEN = "hykb_fcm_open"

        /**
         * 防沉迷的当前无累计时长的键值key
         */
        private const val KEY_FCM_NOT_TIME = "hykb_fcm_open"

        /**
         * 防沉迷游戏当前的方向的key
         */
        private const val KEY_FCM_ORIENTATION = "key_fcm_orientation"

        /**
         * 返回YMStorage对象
         */
        val instance: YMStorage
            get() = SingletonHolder.INSTANCE
    }
}